﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFNote.Services.IDal;

namespace WPFNote.Services
{
    public class ServiceBasic<T, TS> : IServiceBasic<T, TS> where T : Models.BaseEntity, new()
        where TS : Models.BaseEntitySch, new()
    {
        //从子类的构造函数中传入
        protected Repository.IDal.IRepositoryBasic<T, TS> Dal;

        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(T t)
        {
            return await Dal.InsertAsync(t);
        }

        /// <summary>
        /// 批量写入
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(T[] t)
        {
            return await Dal.InsertAsync(t);
        }

        /// <summary>
        /// 写入并返回ID
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> InsertReturnIdAsync(T t)
        {
            return await Dal.InsertReturnIdAsync(t);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T t)
        {
            return await Dal.UpdateAsync(t);
        }

        public async Task<bool> UpdateAsync(T t, TS ts)
        {
            return await Dal.UpdateAsync(t, ts);
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T[] t)
        {
            return await Dal.UpdateAsync(t);
        }

        public async Task<bool> UpdateAsync(T[] t, TS ts)
        {
            return await Dal.UpdateAsync(t, ts);
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            return await Dal.DeleteAsync(id);
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(T t, TS ts)
        {
            return await Dal.DeleteAsync(t, ts);
        }

        /// <summary>
        /// 多个删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(string[] ids)
        {
            return await Dal.DeleteAsync(ids);
        }

        /// <summary>
        /// 根据ID查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> SelectAsync(int id)
        {
            return await Dal.SelectAsync(id);
        }

        /// <summary>
        /// 自定义条件单个查询
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<T> SelectAsync(TS ts)
        {
            return await Dal.SelectAsync(ts);
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> SelectListAsync()
        {
            return await Dal.SelectListAsync();
        }

        /// <summary>
        /// 自定义条件查询列表
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<List<T>> SelectListAsync(TS ts)
        {
            return await Dal.SelectListAsync(ts);
        }

        /// <summary>
        /// 分页查询全部数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public async Task<List<T>> SelectListAsync(int page, int size, RefAsync<int> total)
        {
            return await Dal.SelectListAsync(page, size, total);
        }

        /// <summary>
        /// 自定义查询条件分页查询列表
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public async Task<List<T>> SelectListAsync(TS ts, int page, int size, RefAsync<int> total)
        {
            return await Dal.SelectListAsync(ts, page, size, total);
        }
    }
}
